package edu.uit.se.laps.web.view;

import java.security.KeyStore.LoadStoreParameter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.uit.se.laps.web.controller.LoaiSanPhamManager;
import edu.uit.se.laps.web.controller.ShoppingCart;
import edu.uit.se.laps.web.model.LoaiSanPham;
import edu.uit.se.laps.web.model.SanPham;

public class LoaiSanPhamAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5846719978885963487L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private LoaiSanPham loaiSp;
	private List<LoaiSanPham> loaiSpList;

	private LoaiSanPhamManager loaiSPManager;

	public LoaiSanPhamAction() {
		loaiSPManager = new LoaiSanPhamManager();
	}

	public String execute() {
		this.loaiSpList = loaiSPManager.list();
		System.out.println("execute called");
		return SUCCESS;
	}

	public String addLoaiSP() {
		String tenLoaiSp = request.getParameter("tenLoaiSP");

		LoaiSanPham loaisp = new LoaiSanPham();
		loaisp.setTenLoaiSP(tenLoaiSp);

		try {
			loaiSPManager.addLoaiSanPham(loaisp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.loaiSpList = loaiSPManager.list();
		return SUCCESS;
	}

	public String showupdate() {
		// Lay id sp tu request
		String idLoaiSP_str = request.getParameter("idLoaiSP");

		int id = Integer.parseInt(idLoaiSP_str);
		LoaiSanPham loaisp = loaiSPManager.getLoaiSanPham(id);

		HttpSession session = request.getSession();
		// get the cart
		ShoppingCart shopCart = (ShoppingCart) session
				.getAttribute("ShoppingCart");
		shopCart.setLoaiSPTemp(loaisp);
		session.setAttribute("ShoppingCart", shopCart);

		return SUCCESS;
	}

	public String updateLoaiSP(){
		String idLoaiSp = request.getParameter("idLoaiSP");
		String tenLoaiSp = request.getParameter("tenLoaiSP");
		
		int id = Integer.parseInt(idLoaiSp);
		
		LoaiSanPham loaisp = new LoaiSanPham();
		loaisp.setIdLoaiSP(id);
		loaisp.setTenLoaiSP(tenLoaiSp);

		try {
			loaiSPManager.updateLoaiSanPham(loaisp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.loaiSpList = loaiSPManager.list();
		return SUCCESS;
	}
	
	
	
	public LoaiSanPham getLoaiSp() {
		return loaiSp;
	}

	public void setLoaiSp(LoaiSanPham loaiSp) {
		this.loaiSp = loaiSp;
	}

	public List<LoaiSanPham> getLoaiSpList() {
		return loaiSpList;
	}

	public void setLoaiSpList(List<LoaiSanPham> loaiSpList) {
		this.loaiSpList = loaiSpList;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
